package com.mlog.njyjy.ndop.monitor.collector.controller;

import com.mlog.njyjy.mp.starter.maws.response.Resp;
import com.mlog.njyjy.ndop.monitor.collector.service.CollectWayService;
import com.mlog.njyjy.ndop.monitor.collector.service.TaskService;
import com.mlog.njyjy.ndop.monitor.common.domain.CollectWay;
import com.mlog.njyjy.ndop.monitor.common.domain.Task;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "监控任务管理")
@RestController
@RequestMapping("/api")
@CrossOrigin("*")
public class AdminController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private CollectWayService collectWayService;

    @ApiOperation(value = "查询全部监控任务", tags = "监控任务管理")
    @GetMapping("/tasks")
    public Resp findAll() {
        List<Task> tasks = taskService.findAll();
        if (tasks != null) {
            return Resp.of(tasks);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "查询监控任务详情", tags = "监控任务管理")
    @GetMapping("/tasks/{id}")
    public Resp findOne(@PathVariable int id) {
        Task task = taskService.findOne(id);
        if (task != null) {
            return Resp.of(task);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "保存监控任务", tags = "监控任务管理")
    @PostMapping("/tasks")
    public Resp save(@RequestBody Task task) {
        boolean succeed = taskService.save(task);
        return succeed ? Resp.OK : Resp.FAIL;
    }

    @ApiOperation(value = "移除监控任务", tags = "监控任务管理")
    @DeleteMapping("/tasks")
    public Resp remove(@RequestParam int id) {
        boolean succeed = taskService.delete(id);
        return succeed ? Resp.OK : Resp.FAIL;
    }


    @ApiOperation(value = "查询全部采集配置", tags = "采集配置管理")
    @GetMapping("/collectWays")
    public Resp findAllWays() {
        List<CollectWay> collectWays = collectWayService.findAll();
        if (collectWays != null) {
            return Resp.of(collectWays);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "查询采集配置详情", tags = "采集配置管理")
    @GetMapping("/collectWays/{id}")
    public Resp findOneWay(@PathVariable int id) {
        CollectWay collectWay = collectWayService.findOne(id);
        if (collectWay != null) {
            return Resp.of(collectWay);
        }
        return Resp.FAIL;
    }

    @ApiOperation(value = "保存采集配置", tags = "采集配置管理")
    @PostMapping("/collectWays")
    public Resp saveWay(@RequestBody CollectWay way) {
        boolean succeed = collectWayService.save(way);
        return succeed ? Resp.OK : Resp.FAIL;
    }

    @ApiOperation(value = "移除采集配置", tags = "采集配置管理")
    @DeleteMapping("/collectWays")
    public Resp removeWay(@RequestParam int id) {
        boolean succeed = collectWayService.delete(id);
        return succeed ? Resp.OK : Resp.FAIL;
    }

}
